Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix z-fighting and performance issues in 2D infinite scroll mode #12488

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

andrewda
Copy link
Contributor

Description

This pull request addresses both performance issues and zIndex issues in 2D infinite scroll mode.

Currently, the execute2DViewportCommands function clones camera.position so that the camera's x and y positions can be modified to enable infinite scrolling (i.e., rendering another copy of the map to the side). At the end of execute2DViewportCommands, the camera's position is reset to the copy that was created earlier, because the camera modifications made were solely for rendering -- the camera is not actually intended to move. However, it appears that copying the previous camera z position (which is not modified in execute2DViewportCommands for infinite scroll rendering) resets changes made in executeCommands to avoid z-fighting:

if (scene.mode === SceneMode.SCENE2D) {
// To avoid z-fighting in 2D, move the camera to just before the frustum
// and scale the frustum depth to be in [1.0, nearToFarDistance2D].
camera.position.z = height2D - frustumCommands.near + 1.0;

This pull request fixes this issue by only resetting the x and y values of the camera, rather than the entire position object. This fixes z-fighting reported in #8302 (zIndex Sandcastle):

Before After
CleanShot 2025-02-19 at 12 32 09 CleanShot 2025-02-19 at 12 32 25

Additionally, this pull request removes an extraneous clone() operation on the frustum. The frustum variable is cloned from the camera on initialization, and thus does not need to be recloned later:

const frustum = camera.frustum.clone();

These two changes also resolve some performance issues in 2D infinite scroll mode, for reasons that are not entirely clear to me. See the following screen recordings. In the Before recording, the FPS while panning drops from a steady ~100fps to around ~75fps. In the After recording, the FPS starts substantially higher, around ~140fps, and drops to only ~130fps.

Before Change
CleanShot.2025-02-19.at.12.22.32.mp4
After Change
CleanShot.2025-02-19.at.12.23.03.mp4

I am not clear on exactly where this performance hit is coming from, but the improvements appear to be even more noticeable on a lower-spec machine (where panning performance goes from ~30fps before the change to a steady ~90fps after the change).

Issue number and link

zIndex Issue: #8302

Likely related performance issue: #5026

Testing plan

I have tested that this fixes both the zIndex and performance issues on a variety of machines using this zIndex Sandcastle.

Author checklist

  • I have submitted a Contributor License Agreement
  • I have added my name to CONTRIBUTORS.md
  • I have updated CHANGES.md with a short summary of my change
  • I have added or updated unit tests to ensure consistent code coverage
  • I have updated the inline documentation, and included code examples where relevant
  • I have performed a self-review of my code

Copy link

Thank you for the pull request, @andrewda!

✅ We can confirm we have a CLA on file for you.

@andrewda
Copy link
Contributor Author

This is still a work in progress while I reconcile getting tests to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants